import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog
import os
import requests
from bs4 import BeautifulSoup
def download_files(url, file_type, save_path):
    try:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        for link in soup.find_all('a', href=True):
            href = link['href']
            if href.endswith(file_type): 
                full_url = urljoin(url, href)
                r = requests.get(full_url, stream=True)
                filename = os.path.join(save_path, os.path.basename(href))
                with open(filename, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=8192):
                        if chunk:
                            f.write(chunk)
                print(f"Downloaded: {filename}")

    except Exception as e:
        messagebox.showerror("Error", str(e))
def main():
    root = tk.Tk()
    root.withdraw()
    url = simpledialog.askstring("Input", "Enter the URL:")
    file_type = simpledialog.askstring("Input", "Enter the file extension (e.g., .pdf):")
    save_path = filedialog.askdirectory()
    download_files(url, file_type, save_path) 
    root.destroy()
if __name__ == "__main__":
    from urllib.parse import urljoin
    main()
